<?php

namespace App\Http\Controllers\Frontend;


use App\Logics\NewsLogic;
use App\Logics\SiteLogic;
use Illuminate\Support\Facades\Config;


class CateController extends BaseController
{
    // 栏目页 初始化
    public function initialize()
    {
        parent::initialize();
    }

    // 首页
    public function index()
    {
        // 栏目信息
        $info   = $this->getCateInfo();
        $cateId = $info['id'] ?? 0;
        $config = $this->config($cateId);

        // 整理栏目配置信息
        if($info){
            $info['description'] = $config['description'] ?? '';
            $info['keywords']    = $config['keywords'] ?? '';
        }


        $option    = $this->optLogic();
        $component = new Components([]);
        // 组合插件
        $res    = $component->cate($cateId, 1);
        $html   = $res['listHtml'];
        $min_id = $res['min_id'];
        $max_id = $res['max_id'];

        // 传值
        $nextPage = $this->nextPage('cate', $info);

        $option->assign('listHtml', $html);
        $option->assign('total', $res['total']??0);
        $option->assign('rows', $res['rows']);
        $option->assign('min_id', $min_id);
        $option->assign('max_id', $max_id);
        $option->assign('cateId', $cateId);
        $option->assign('info', $info);
        $option->assign('nextPage', $nextPage);

        return $this->dispaly('frontend.cate');
    }

    // 获取更多
    public function more()
    {
        // 栏目信息
        $info   = $this->getCateInfo();
        $cateId = $info['id'] ?? 0;
        $config = $this->config($cateId);

        // 整理栏目配置信息
        if($info){
            $info['description'] = $config['description'] ?? '';
            $info['keywords']    = $config['keywords'] ?? '';
        }

        $nextPage  = $this->nextPage('cate', $info);
        $component = new Components([]);
        // 组合插件
        $res    = $component->cate($cateId);
        $html   = $res['listHtml'];
        $min_id = $res['min_id'];
        $max_id = $res['max_id'];

        return $this->success(['html' => (string)$html, 'nextUrl' => $nextPage, 'min_id' => $min_id, 'max_id' => $max_id]);
    }

    // 获取最新
    public function newest()
    {
        // 栏目信息
        $info   = $this->getCateInfo();
        $cateId = $info['id'] ?? 0;

        // 栏目ID 可能直接传递过来的
        $option = $this->optLogic();
        if(empty($cateId)){
            $cateId = $option->getData('cate_id', 0, 'intval');
        }

        $option->setField('cate_id', $cateId);
        $option->setField('rows', 15);
        $option->setField('is_count', null);  // 不进行统计
        $option->setField('is_new', 1);  //
        $option->setField('is_first', 1);  // 返回详情第一段
        $option->setField('title', null);  // 避免页面传值的时候进行搜索


        $logic  = new NewsLogic();
        $result = $logic->onLists($this->myUid);
        $max_id = empty($result['list']) ? 0 : max(array_column($result['list'], 'id'));
        $min_id = empty($result['list']) ? 0 : min(array_column($result['list'], 'id'));
        $m_time = empty($result['list']) ? 0 : max(array_column($result['list'], 'ctime'));

        // 结果整理
        //$result['nList']      = $this->makeNewsLists($result['list']);
        //$result['nList']      = $result['list'];
        $result['style_host'] = env('STYLE_HOST');
        $result['app_host']   = request()->getSchemeAndHttpHost().'/';


        $html = $option->dispaly('frontend.component.lists', $result, false);


        return $this->success(['html' => (string)$html, 'max_id' => $max_id, 'max_time' => $m_time,]);
    }

    // 获取栏目id
    protected function getCateInfo()
    {
        $option = $this->optLogic();
        $title  = $option->getData('title', null, 'trim');
        $page   = $option->getData('page', 1, 'intval');

        // 栏目id
        $info = [];
        if($title){
            $title = strtolower(str_replace('-', ' ', $title));
            $lists = (new SiteLogic())->cates('rank ASC', [['status', '>=', 0]]);
            foreach($lists as $item){
                if(strtolower($item['en_title']) == $title){
                    $info = $item;
                    break;
                }
            }
        }

        return $info;
    }

    // 栏目的各种配置  如果没有配置，则获取首页的配置
    protected function config($cateId = 0)
    {
        // cn_title  en_keyword
        // description  keywords
        $info = (new SiteLogic())->cateInfo($cateId);
        if(empty($info)){
            return [];
        }

        $config = ['description' => '', 'keywords' => $info['en_keyword']];

        return $config;
    }


    // 栏目的各种配置  如果没有配置，则获取首页的配置
    protected function config2($cateId = 0)
    {
        $config = Config::get('cate');

        return $config[$cateId] ?? $config[0];
    }

}


